﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YJKRevitCode.ElmentSearchUtility.LinkDocumentSuite;
using YJKRGeometry;
using SectionSignAndFilled.StairsRoomSection.Kit;
using SectionSignAndFilled.StairsRoomSection.Kit.Xdata;

namespace SectionSignAndFilled.StairsRoomSection.SignElems
{
	public class StairsRailingSign
	{
		public StairsRailingSign(Autodesk.Revit.DB.Document doc, List<BaseLinkDocumentResultBean> lstRailing, List<BaseLinkDocumentResultBean> lstStaris)
		{
			this.m_doc = doc;
			this.m_lstRailing = new List<BaseLinkDocumentResultBean>();
			this.m_lstRailing.AddRange(lstRailing);
			this.m_listStairs = new List<BaseLinkDocumentResultBean>();
			this.m_listStairs.AddRange(lstStaris);
		}

		public bool Sign()
		{
			this.m_lstRailing.Sort(new Comparison<BaseLinkDocumentResultBean>(new RailingComparison().Compare));
			bool flag = this.CheckRailingHeightEqual();
			if (this.m_lstRailing.Count == 0)
			{
				return true;
			}
			if (flag)
			{
				this.SignSingleRailing(this.m_lstRailing[0]);
			}
			else
			{
				foreach (BaseLinkDocumentResultBean railing in this.m_lstRailing)
				{
					this.SignSingleRailing(railing);
				}
			}
			return true;
		}

		private bool CheckRailingHeightEqual()
		{
			bool result = true;
			double val = 0.0;
			int num = 0;
			for (int i = 0; i < this.m_lstRailing.Count; i++)
			{
				Railing railing = this.m_lstRailing[i].UseElement as Railing;
				RailingType railingType = railing.Document.GetElement(railing.GetTypeId()) as RailingType;
				Parameter parameter = railingType.get_Parameter(BuiltInParameter.RAILING_SYSTEM_TOP_RAIL_HEIGHT_PARAM);
				if (parameter != null)
				{
					double num2 = parameter.AsDouble();
					if (num == 0)
					{
						val = num2;
					}
					else if (!Geometry.IsEqual(val, num2))
					{
						result = false;
						break;
					}
					num++;
				}
			}
			return result;
		}

		private bool SignSingleRailing(BaseLinkDocumentResultBean Railing)
		{
			Railing railing = Railing.UseElement as Railing;
			Document document = railing.Document;
			double num = 0.0;
			if (!this.GetStairsRiserHeight(Railing, ref num))
			{
				return false;
			}
			RailingType railingType = document.GetElement(railing.GetTypeId()) as RailingType;
			Parameter parameter = railingType.get_Parameter(BuiltInParameter.STAIRS_RAILING_HEIGHT);
			double num2 = parameter.AsDouble();
			List<Curve> list = railing.GetPath() as List<Curve>;
			list = CommonFun.TranCurve(list, Railing.GetTotalTransform());
			Curve curve = list[0];
			XYZ xyz = (curve as Line).Direction.Normalize().Negate();
			XYZ endPoint = curve.GetEndPoint(0);
			XYZ xyz2 = new XYZ(endPoint.X, endPoint.Y, endPoint.Z + num);
			XYZ xyz3 = xyz2 + xyz * 0.5 * SignCommon.m_dBaseLineInterval;
			Line line = Line.CreateBound(xyz2, xyz3);
			DetailCurve detailCurve = this.m_doc.Create.NewDetailCurve(this.m_doc.ActiveView, line);
			if (detailCurve != null)
			{
				SignXdataForReference.WriteSignComment(detailCurve, this.m_doc.ActiveView.Id);
			}
			XYZ xyz4 = xyz2 + XYZ.BasisZ * num2;
			XYZ xyz5 = xyz4 + xyz * 0.5 * SignCommon.m_dBaseLineInterval;
			Line line2 = Line.CreateBound(xyz4, xyz5);
			DetailCurve detailCurve2 = this.m_doc.Create.NewDetailCurve(this.m_doc.ActiveView, line2);
			if (detailCurve2 != null)
			{
				SignXdataForReference.WriteSignComment(detailCurve2, this.m_doc.ActiveView.Id);
			}
			Line line3 = Line.CreateBound(xyz3, xyz5);
			ReferenceArray referenceArray = new ReferenceArray();
			referenceArray.Append(new Reference(detailCurve));
			referenceArray.Append(new Reference(detailCurve2));
			ElementId dimSymbol = SignCommon.GetDimSymbol(this.m_doc);
			Dimension dimension = this.m_doc.Create.NewDimension(this.m_doc.ActiveView, line3, referenceArray, this.m_doc.GetElement(dimSymbol) as DimensionType);
			if (dimension != null)
			{
				SignXdataForReference.WriteSignComment(dimension, this.m_doc.ActiveView.Id);
			}
			return true;
		}

		private bool GetStairsRiserHeight(BaseLinkDocumentResultBean beanRail, ref double dRiserHeight)
		{
			Railing railing = beanRail.UseElement as Railing;
			Document document = railing.Document;
			ElementId hostId = railing.HostId;
			if (hostId != ElementId.InvalidElementId)
			{
				Stairs stairs = document.GetElement(hostId) as Stairs;
				dRiserHeight = stairs.ActualRiserHeight;
				return true;
			}
			List<Curve> list = railing.GetPath() as List<Curve>;
			Transform totalTransform = beanRail.GetTotalTransform();
			list = CommonFun.TranCurve(list, totalTransform);
			Curve curve = list[0];
			double z = curve.GetEndPoint(0).Z;
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in this.m_listStairs)
			{
				Stairs stairs2 = baseLinkDocumentResultBean.UseElement as Stairs;
				Transform totalTransform2 = baseLinkDocumentResultBean.GetTotalTransform();
				double val = SignCommon.TransformDouble(totalTransform2, stairs2.BaseElevation);
				double val2 = SignCommon.TransformDouble(totalTransform2, stairs2.TopElevation);
				if (!Geometry.LessThan(z, val) && !Geometry.GreaterThan(z, val2))
				{
					dRiserHeight = stairs2.ActualRiserHeight;
					return true;
				}
			}
			return false;
		}

		private Document m_doc;

		private List<BaseLinkDocumentResultBean> m_lstRailing;

		private List<BaseLinkDocumentResultBean> m_listStairs;
	}
}
